﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using FTchina;
using System.IO;
namespace FTchina.Api
{
    /// <summary>
    ///htmlpath 的摘要说明
    /// </summary>
    public static class htmlpath
    {

        /// <summary>
        /// 根据文章id 返回html文件相对路径
        /// </summary>
        /// <param name="artid"></param>
        /// <returns>相对路径</returns>
        public static string GetHtmlPath(string artid)
        {
            return GetHtmlPath(artid, "1");
        }




        /// <summary>
        /// 根据文章id、分页id返回html文件相对路径
        /// </summary>
        /// <param name="artid"></param>
        /// <param name="p"></param>
        /// <returns>相对路径</returns>
        public static string GetHtmlPath(string artid, string p)
        {
            DateTime date = Convert.ToDateTime(shujuku.GetValueByKey("data", "content", "id='" + artid + "'"));
            string year = date.Year.ToString();
            string month = date.Month.ToString();
            string day = date.Day.ToString();
            string path = "html/" + year + "/" + month + "/" + day + "/";
            if (!Directory.Exists(basic.GetMapPath() + path)) Directory.CreateDirectory(basic.GetMapPath() + path);
            p = (p.Length == 0) ? "1" : p;
            string filename = artid + "_" + p + ".aspx";
            return path + filename;
        }


        /// <summary>
        /// 获取文件路径
        /// </summary>
        /// <param name="artid">文章id</param>
        /// <param name="p">分页id</param>
        /// <param name="date">时间</param>
        /// <returns>相对路径</returns>
        public static string GetHtmlPath(string artid, string p, string dates)
        {
            try
            {
                DateTime date = Convert.ToDateTime(dates);
                string year = date.Year.ToString();
                string month = date.Month.ToString();
                string day = date.Day.ToString();
                string path = "html/" + year + "/" + month + "/" + day + "/";
                if (!Directory.Exists(basic.GetMapPath() + path)) Directory.CreateDirectory(basic.GetMapPath() + path);
                p = (p.Length == 0) ? "1" : p;
                string filename = artid + "_" + p + ".aspx";
                return path + filename;


            }
            catch
            {
                return GetHtmlPath(artid, p);
            }

        }

        /// <summary>
        /// 获取完整的页面地址
        /// </summary>
        /// <param name="artid">文章id</param>
        /// <param name="p">分页id</param>
        /// <param name="dates">日期</param>
        /// <returns>http://www.weibaohui.com/html/2009/6/9/12_1.html</returns>
        public static string GetFullPath(string artid, string p, string dates)
        {
            //因为实现统一的url地址所以隐藏了静态页面的地址，故注释掉下面。
            //if (basic.IsHtml)
            //{
            //    DateTime date = Convert.ToDateTime(dates);
            //    string year = date.Year.ToString();
            //    string month = date.Month.ToString();
            //    string day = date.Day.ToString();
            //    string path = basic.GetSiteUrl() + "html/" + year + "/" + month + "/" + day + "/";
            //    p = (p.Length == 0) ? "1" : p;
            //    string filename = artid + "_" + p + ".aspx";
            //    return path + filename;
            //}
            //else
            //{
            //    return basic.GetSiteUrl() + "site/s" + artid + ".aspx";
            //}


            return basic.GetSiteUrl() + "site/s" + artid + ".aspx";

        }


        /// <summary>
        /// 获取完整的页面地址
        /// </summary>
        /// <param name="artid"></param>
        /// <param name="p"></param>
        /// <returns>http://www.weibaohui.com/html/2009/6/9/12_1.html</returns>
        public static string GetFullPath(string artid, string p)
        {
            //因为实现统一的url地址所以隐藏了静态页面的地址，故注释掉下面。

            //if (basic.IsHtml)
            //{
            //    DateTime date = Convert.ToDateTime(shujuku.GetValueByKey("data", "content", "id='" + artid + "'"));
            //    string year = date.Year.ToString();
            //    string month = date.Month.ToString();
            //    string day = date.Day.ToString();
            //    string path = basic.GetSiteUrl() + "html/" + year + "/" + month + "/" + day + "/";
            //    p = (p.Length == 0) ? "1" : p;
            //    string filename = artid + "_" + p + ".aspx";
            //    return path + filename;
            //}
            //else
            //{
            //    return basic.GetSiteUrl() + "site/s" + artid + ".aspx";
            //}

            // return basic.GetSiteUrl() + "site/s" + artid + ".aspx";
            string temp = basic.GetSiteUrl() + "site/s" + artid + ".aspx";
            return (p == "1") ? temp : temp + "?pageindex=" + p;

        }
        /// <summary>
        /// 删除指定id的html静态文件
        /// </summary>
        /// <param name="artid"></param>
        public static void DeleteHtmls(string artid)
        {


            string BigPager = (shujuku.testread("select pageindex from contentpage where artid='" + artid + "' order by pageindex desc")) ? shujuku.GetDataTable("select pageindex from contentpage where artid='" + artid + "' order by pageindex desc").Rows[0][0].ToString().Trim() : "1";
            int endloop = (BigPager.Length == 0) ? 1 : Convert.ToInt32(BigPager);
            try
            {
                for (int i = 1; i <= endloop; i++)
                {
                    string path = basic.GetMapPath() + GetHtmlPath(artid, i.ToString());
                    if (System.IO.File.Exists(path)) System.IO.File.Delete(path);
                }
            }
            catch
            {

            }


        }


        #region 生成静态文件的控制类
        /// <summary>
        /// 生成新的文件
        /// </summary>
        /// <param name="artid">文章ID</param>
        public static void MakeNewHtml(string artid)
        {
            try
            {
                DataTable dt = shujuku.GetDataTable("select id,lanmuid,data from content where id='" + artid + "'");
                string BigPager = (shujuku.testread("select pageindex from contentpage where artid='" + artid + "' order by pageindex desc")) ? shujuku.GetDataTable("select pageindex from contentpage where artid='" + artid + "' order by pageindex desc").Rows[0][0].ToString().Trim() : "1";
                int endloop = (BigPager.Length == 0) ? 1 : Convert.ToInt32(BigPager);
                for (int i = 1; i <= endloop; i++)
                {
                    MakeNewHtml(artid, Convert.ToInt32(dt.Rows[0]["lanmuid"].ToString()), i);
                }
            }
            catch 
            {

            }
        }
        /// <summary>
        /// 生成新文件
        /// </summary>
        /// <param name="artid">文章id</param>
        /// <param name="cid">栏目id</param>
        public static void MakeNewHtml(string artid, int cid)
        {
            try
            {
                string BigPager = (shujuku.testread("select pageindex from contentpage where artid='" + artid + "' order by pageindex desc")) ? shujuku.GetDataTable("select pageindex from contentpage where artid='" + artid + "' order by pageindex desc").Rows[0][0].ToString().Trim() : "1";
                int endloop = (BigPager.Length == 0) ? 1 : Convert.ToInt32(BigPager);
                for (int i = 1; i <= endloop; i++)
                {
                    MakeNewHtml(artid, cid, i);
                }
            }
            catch { return; }
        }

        /// <summary>
        /// 生成新文件
        /// </summary>
        /// <param name="artid">文章id</param>
        /// <param name="cid">栏目id</param>
        /// <param name="pageindex">分页id</param>
        public static void MakeNewHtml(string artid, int cid, int pageindex)
        {
            FTchina.PureHtml.CreatHtml cr = new FTchina.PureHtml.CreatHtml();
            cr.CreatOneHtml(artid, cid, pageindex);

        }
        /// <summary>
        /// 生成ID号最大的文章的静态文件
        /// </summary>
        public static void MakeNewHtmlByBiggestID()
        {
            string bigid = shujuku.GetDataTable("select max(id) from content").Rows[0][0].ToString();
            MakeNewHtml(bigid);
        }

        /// <summary>
        /// 按照分类生成静态文件
        /// </summary>
        /// <param name="Fid"></param>
        public static void MakeHtmlByFenlei(string Fid)
        {
            DataTable dt = shujuku.GetDataTable("select id,lanmuid,data from content where lanmuid='" + Fid + "'");
            foreach (DataRow dr in dt.Rows)
            {
                MakeNewHtml(dr["id"].ToString(), Convert.ToInt32(dr["lanmuid"].ToString()));
            }
        }

        /// <summary>
        ///按照ID区间生成静态文件
        /// </summary>
        /// <param name="smallid"></param>
        /// <param name="bigid"></param>
        public static void MakeHtmlByIDs(string smallid, string bigid)
        {
            DataTable dt = shujuku.GetDataTable("select id,lanmuid,data from content where (id>=" + smallid + " and id<=" + bigid + ")");
            foreach (DataRow dr in dt.Rows)
            {
                MakeNewHtml(dr["id"].ToString(), Convert.ToInt32(dr["lanmuid"].ToString()));

            }
        }

        /// <summary>
        /// 按照日期生成静态文件
        /// </summary>
        /// <param name="smalldate"></param>
        /// <param name="bigdate"></param>
        public static void MakeHtmlByDates(string smalldate, string bigdate)
        {
            DataTable dt = shujuku.GetDataTable("select id,lanmuid,data from content where (data>='" + smalldate + "' and data<='" + bigdate + "')");
            foreach (DataRow dr in dt.Rows)
            {
                MakeNewHtml(dr["id"].ToString(), Convert.ToInt32(dr["lanmuid"].ToString()));

            }

        }
        /// <summary>
        /// 重建所有的HTML文件
        /// </summary>
        public static void ReMakeAllHtmls()
        {
            DataTable dt = shujuku.GetDataTable("select id,lanmuid,data from content order by id desc");
            foreach (DataRow dr in dt.Rows)
            {
                MakeNewHtml(dr["id"].ToString(), Convert.ToInt32(dr["lanmuid"].ToString()));
            }

        }

        #endregion
        /// <summary>
        /// 根据类id，分页id获取列表相对路径
        /// </summary>
        /// <param name="artid"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        public static string GetListPath(string cid, string p)
        {
            string path = "html/list/";
            if (!Directory.Exists(basic.GetMapPath() + path)) Directory.CreateDirectory(basic.GetMapPath() + path);
            p = (p.Length == 0) ? "1" : p;
            string filename = "list" + cid + "_" + p + ".aspx";
            return path + filename;
        }


        /// <summary>
        /// 检查静态文件是否过期
        /// </summary>
        /// <returns></returns>
        public static void ChekHtmlDate(string path)
        {
            FTchina.SiteConfig.SiteInfo si = new FTchina.SiteConfig.SiteInfo();
            int RefreshTime = Convert.ToInt32(si.RefreshTime);
            FileInfo fi = new FileInfo(basic.GetMapPath() + path);
            if (DateTime.Now.Subtract(fi.LastWriteTime).TotalMinutes > RefreshTime)
            { fi.Delete(); }

        }

        /// <summary>
        /// 删除分类页面html文件
        /// </summary>
        public static void DeleteListHtml()
        {
            try
            {
                System.IO.Directory.Delete(basic.GetMapPath() + "html/list/", true);
            }
            catch { }
        }

        /// <summary>
        /// 重建首页
        /// </summary>
        public static void RebuidHome()
        {
            try
            {
                System.IO.File.Delete(basic.GetMapPath() + "site/default.html");
            }
            catch { }
        }


        /// <summary>
        /// 获取当前运行页面的文件路径名称
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentPagePath()
        {
            string tempwords = HttpContext.Current.Request.Path;
            tempwords = tempwords.Substring(1, tempwords.Length - 1);
            return tempwords;
        }
    }
}